﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.Data;
using System.IO;
using System.Configuration;

using Microsoft.Reporting.WebForms;
using System.Security.Principal;
using System.Net;
using System.Threading;
using System.Globalization;

using System.Reflection;

using Banesco.Entidades;


namespace WebCliente.Pages
{
    public partial class FrmReportesPersonaNatural : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                CargarReporteDefault();
            }

        }

        protected void Page_Init(object sender, EventArgs e)
        {
            ScriptManager sm = ScriptManager.GetCurrent(this);
            if (sm != null)
            {
                sm.EnablePartialRendering = false;
            }
        }

        protected override void InitializeCulture()
        {
            if (!IsPostBack)
            {
                CultureInfo culture = new CultureInfo("en-US");
                culture.DateTimeFormat.ShortDatePattern = "MM/dd/yyyy";
                culture.NumberFormat.CurrencySymbol = "$";
                Thread.CurrentThread.CurrentCulture = culture;
                Thread.CurrentThread.CurrentUICulture = culture;
            }
        }

        private void CargarReporteDefault()
        {
            string servidoReporteURL = ConfigurationManager.AppSettings["ServidorReporteURL"].ToString();
            string reporteUbicacion = ConfigurationManager.AppSettings["ReportePersonaNatural"].ToString();
            string idCliente = Request.QueryString["IDCliente"];

            try
            {
                Uri objUrl = new Uri(servidoReporteURL);

                ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
                ReportViewer1.ShowCredentialPrompts = false;

                ReportViewer1.ServerReport.ReportServerCredentials =
                            new MyReportServerCredentialsRPN();

                ReportViewer1.ServerReport.ReportServerUrl = objUrl;
                ReportViewer1.ServerReport.ReportPath = reporteUbicacion;
                List<ReportParameter> parameters = new List<ReportParameter>();

                parameters.Add(new ReportParameter("IdCliente", idCliente));


                ReportViewer1.ServerReport.SetParameters(parameters);
                ReportViewer1.ProcessingMode = ProcessingMode.Remote;
                ReportViewer1.ShowParameterPrompts = false;
                ReportViewer1.ShowPromptAreaButton = false;
                DesabilitarFormatosExportacion(ReportViewer1.ServerReport, false);
                //ReportViewer1.PromptAreaCollapsed = true;
                //ReportViewer1.ShowToolBar = false;
                ReportViewer1.ServerReport.Refresh();
                ReportViewer1.DataBind();
            }
            catch (Exception ex)
            { }
        }

        public void DesabilitarFormatosExportacion(ServerReport reportServer, bool activar)
        {
            foreach (RenderingExtension extension in reportServer.ListRenderingExtensions())
            {
                if (extension.Name == "XML" || extension.Name == "IMAGE" || extension.Name == "MHTML" || extension.Name == "CSV" || extension.Name == "EXCEL" || extension.Name == "WORD")
                {
                    ReflectivelySetVisibilityFalse(extension, activar);
                }

            }

        }

        private void ReflectivelySetVisibilityFalse(RenderingExtension extension, bool activar)
        {
            try
            {
                FieldInfo info = extension.GetType().GetField("m_isVisible",
                                                              BindingFlags.NonPublic
                                | BindingFlags.Instance);
                if (info != null)
                {
                    info.SetValue(extension, activar);
                }
            }
            catch (Exception ex)
            {

            }

        }

        protected void btnImprimirTitulares_Click(object sender, EventArgs e)
        {
            string url = Constantes.PAGINAIMPRESIONTITULARES + "?IdCliente=" + Request.QueryString["IDCliente"];
            Response.Redirect(url);
        }

    }

    [Serializable]
    public sealed class MyReportServerCredentialsRPN :
        IReportServerCredentials
    {
        public WindowsIdentity ImpersonationUser
        {
            get
            {
                // Use the default Windows user.  Credentials will be
                // provided by the NetworkCredentials property.
                return null;
            }
        }

        public ICredentials NetworkCredentials
        {
            get
            {
                // Read the user information from the Web.config file.  
                // By reading the information on demand instead of 
                // storing it, the credentials will not be stored in 
                // session, reducing the vulnerable surface area to the
                // Web.config file, which can be secured with an ACL.

                // User name
                string userName =
                    ConfigurationManager.AppSettings
                        ["ReportViewerUser"];

                if (string.IsNullOrEmpty(userName))
                    throw new Exception(
                        "Missing user name from web.config file");

                // Password
                string password =
                    ConfigurationManager.AppSettings
                        ["ReportViewerPassword"];

                if (string.IsNullOrEmpty(password))
                    throw new Exception(
                        "Missing password from web.config file");

                // Domain
                string domain =
                    ConfigurationManager.AppSettings
                        ["ReportViewerDomain"];


                //if (string.IsNullOrEmpty(domain))
                //    throw new Exception(
                //        "Missing domain from web.config file");

                return new NetworkCredential(userName, password, domain);
            }
        }

        public bool GetFormsCredentials(out Cookie authCookie,
                    out string userName, out string password,
                    out string authority)
        {
            authCookie = null;
            userName = null;
            password = null;
            authority = null;

            // Not using form credentials
            return false;
        }
    }

}
